﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SPMS.Log.Core;

namespace SPMS.Log.Appender
{
    /// <summary>
    /// 回滚文件日志记录器
    /// </summary>
    /// <remarks>根据回滚策略设定，在满足回滚策略时自动生成一个新日志文件的文件日志记录器</remarks>
    /// <history>
    /// [zanezeng]               2009/9/28 13:48    创建
    /// </history>
    public class RollingFileLogAppender : FileLogAppender
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="RollingFileLogAppender"/>的新实例
        /// </summary>
        /// <param name="name">当前日志记录器的名称</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/10 10:13    创建
        /// </history>
        public RollingFileLogAppender( string name )
            : base( name )
        {
        }

        #endregion

        #region ---Protected Method

        /// <summary>
        /// 通知准备输出给定的日志条目
        /// </summary>
        /// <param name="entry">要输出的日志条目</param>
        /// <returns>
        /// 	<c>True</c>：允许输出给定的日志条目<br/>
        /// 	<c>False</c>：不允许输出给定的日志条目
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/28 9:25    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/28 10:56    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/10 15:34    创建
        /// </history>
        protected override bool OnBeforeAppend( LogEntry entry )
        {
            //用于保存计划日志文件名称
            string scheduledFileName = string.Empty;

            //判断是否需要回转日志文件
            if (this.RollingStrategy.RollFile( this.FileStream, out scheduledFileName ))
            {
                //重置文件名称
                this.FileName = scheduledFileName;
            }

            //调用基类实现
            return base.OnBeforeAppend( entry );
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得或者设置日志文件回转策略
        /// </summary>
        /// <value>日志文件回转策略</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/10 14:49    创建
        /// </history>
        public IFileRollStrategy RollingStrategy
        {
            get
            {
                return (null == this.m_RollingStrategy) ? DEFAULT_FILEROLLSTRATEGY : this.m_RollingStrategy;
            }
            set
            {
                this.m_RollingStrategy = value;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 日志文件的回转策略
        /// </summary>
        private IFileRollStrategy m_RollingStrategy;

        /// <summary>
        /// 默认的文件回转策略
        /// </summary>
        private static readonly IFileRollStrategy DEFAULT_FILEROLLSTRATEGY = new DailyFileRollStrategy();

        #endregion
    }
}
